package com.xuecheng.media.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xuecheng.media.exception.MediaFileNotConvertedException;
import com.xuecheng.media.exception.MediaFileNotExistException;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.IMediaFilesService;
import com.xuecheng.media.service.IMediaOpenService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @description: 媒资公开查询接口实现类
 * @author 勾新杰
 * @version 1.0
 * @date 2025/2/14 20:52
 */
@Service
@RequiredArgsConstructor
public class MediaOpenServiceImpl implements IMediaOpenService {

    /**
     * 媒资文件服务
     */
    private final IMediaFilesService mediaFilesService;

    /**
     * 根据媒资id查询媒资url
     *
     * @param mediaId 媒资id
     * @return url
     */
    @Override
    public String getUrlByMediaId(String mediaId) {
        MediaFiles mediaFiles = mediaFilesService.getById(mediaId);
        // 1. 媒资文件不存在
        if (mediaFiles == null) MediaFileNotExistException.cast();
        String url = mediaFiles.getUrl();
        // 2. 媒资文件还没有转码
        if (StrUtil.isBlank(url) || !url.endsWith(".mp4"))
            MediaFileNotConvertedException.cast();
        return url;
    }
}
